<?php
/*
 * @Author: LiuHaiL 18655112670@139.com
 * @Date: 2022-06-30 16:36:34
 * @LastEditors: LiuHaiL 18655112670@139.com
 * @LastEditTime: 2022-11-16 08:36:23
 * @FilePath: \ant_form_admind:\laragon\www\ghpt_api\app\api\command\v1\syncUnitTotal.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
declare (strict_types = 1);

namespace app\api\command\v1;

use app\api\helpers\Tree;
use app\api\model\v1\Unit;
use app\api\model\v1\UnitTotal;
use app\api\model\v1\Users;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class syncUnitTotal extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('sync_unit_total')
            ->setDescription('the sync_unit_total command');
    }

    protected function execute(Input $input, Output $output)
    {
        $unit = Unit::select()->toArray();
        foreach ($unit as $key => $value) {
            $output->writeln(sprintf('Synchronize data with ID - '.$value['id'].'-'.$value['unit_title']));
            $where = self::getChildUnit($value['id'], true,$unit);
            $user_where = self::getChildUnit($value['id'], false,$unit);
            $user_count = Users::field('id,username')->where($user_where)->where('is_admin', 0)->count();
            $user_three_count = Users::field('id')->where($user_where)->where('wx_role_id', 3)->where('is_admin', 0)->count();
            $user_four_count = Users::field('id')->where($user_where)->where('wx_role_id', 4)->where('is_admin', 0)->count();
            $res = Unit::field('id')->where($where)->where('level', '=', Unit::LEVEL_THREE)->count();
            $find = UnitTotal::where(['unit_id' => $value['id']])->find();
            if (is_null($find)) {
                UnitTotal::create([
                    'unit_id' => $value['id'],
                    'total' => $res,
                    'persons_total' => $user_count,
                    'answer_persons_total' => ($user_three_count + $user_four_count),
                ]);
                $output->writeln(sprintf('我是新增 - '.$value['id'].'-'.$value['unit_title']));
            } else {
                UnitTotal::where(['unit_id' => $value['id']])->update([
                    'total' => $res,
                    'persons_total' => $user_count,
                    'answer_persons_total' => ($user_three_count + $user_four_count),
                ]);
                $output->writeln(sprintf('我是更新 - '.$value['id'].'-'.$value['unit_title']));
            }

        }
        sleep(3); //睡眠1秒
//        halt("成功");
        // 指令输出
        $output->writeln('sync_unit_total');
    }

    public function getChildUnit($unit_id, $mySelf,$data)
    {
        $tree = new Tree();
        $search = $tree->instance()->init($data, 'pid');
        $children = $search->getChildrenIds(intval($unit_id), true);
        sort($children);
        if ($mySelf) {
            $where[] = ['id', 'in', $children];
        } else {
            $where[] = ['unit_id', 'in', $children];
        }
        return $where;
    }
}
